﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Avalonia.MusicStore
{
    using System.IO;
    using System.Text.RegularExpressions;

    public static class FileSystemHelper
    {
        // 清理文件名，移除所有非法字符
        public static string SanitizeFileName(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
                return "unnamed";

            // 获取操作系统定义的非法字符
            string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
            string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars);

            // 替换非法字符为下划线
            return Regex.Replace(fileName, invalidRegStr, "_");
        }
    }
}
